package io.geekidea.boot.util.baiwangApi;

import com.baiwang.open.client.BWRestClient;
import com.baiwang.open.client.IBWClient;
import com.baiwang.open.client.login.BopLoginClient;
import com.baiwang.open.client.login.BopLoginResponse;
import com.baiwang.open.client.login.PasswordLoginClient;
import com.baiwang.open.client.login.PasswordLoginConfig;
import com.baiwang.open.entity.request.SOutputinvoiceQueryRequest;
import com.baiwang.open.entity.response.SOutputinvoiceQueryResponse;
import com.baiwang.open.exception.BWOpenException;
import io.geekidea.boot.config.baiwang.BaiwangConfig;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

/**
 * 查询开票结果
 */
@Component
public class Query {
    static String url = BaiwangConfig.getUrl();
    static String appKey = BaiwangConfig.getAppKey();
    static String userName = BaiwangConfig.getUserName();
    static String password = BaiwangConfig.getPassword();
    static String appSecret = BaiwangConfig.getAppSecret();
    static String userSalt = BaiwangConfig.getUserSalt();

    public static SOutputinvoiceQueryResponse queryInvoice(
            String taxNo,
            List<String> serialNos,
            List<String> orderNos,
            String detailMark
    ) throws BWOpenException {
        try {
            String requestId = UUID.randomUUID().toString(); //自定义请求唯一标识
            PasswordLoginConfig loginConfig = new PasswordLoginConfig();
            loginConfig.setUrl(url);
            loginConfig.setClientId(appKey);
            loginConfig.setClientSecret(appSecret);
            loginConfig.setUsername(userName);
            loginConfig.setPassword(password);
            loginConfig.setUserSalt(userSalt);
            BopLoginClient loginClient = new PasswordLoginClient(loginConfig);

            // 获取token, token相关说明请参考 文档中心 -> 用户授权说明
            BopLoginResponse loginResponse = loginClient.login();
            // 重载方法，可以传自定义请求唯一标识
            // BopLoginResponse loginResponse = loginClient.login(requestId);
            String token = loginResponse.getResponse().getAccessToken();

            SOutputinvoiceQueryRequest request = new SOutputinvoiceQueryRequest();

            request.setTaxNo(taxNo);//必填  销方税号
            request.setDetailMark(detailMark);//非必填 是否需要发票明细:0-不需要,1-需要(默认不需要)

            //非必填 List<String>, 申请流水号列表(最多支持100个，申请流水号/开票单号，任选一项必填，都传值时，优先使用申请流水号查询)
//            List<String> serialNos = new ArrayList<String>();
//            serialNos.add("25090917103523001844");
            request.setSerialNos(serialNos);

            //非必填 List<String>, 开票单号列表(最多支持100个，申请流水号/开票单号，任选一项必填)
//            List<String> orderNos = new ArrayList<String>();
//            orderNos.add("");
            request.setOrderNos(orderNos);

            IBWClient client = new BWRestClient(url, appKey, appSecret); // 初始化一个客户端
            //IBWClient client = new BWRestClient(url, appKey, appSecret, loginClient); // 初始化一个客户端,构造函数中传入loginConfig对象参数可针对‘token错误’异常进行重试1次。
            SOutputinvoiceQueryResponse response = client.sOutputinvoice().query(request, token);
            // 重载方法，可以传自定义请求唯一标识
            // SOutputinvoiceQueryResponse response = client.sOutputinvoice().query(request, token, requestId);
            System.out.println("response"+response);
            return response;
        } catch (BWOpenException e) {
            throw e;
        }
    }
}